package com.bsj.power.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.entity.log.SysExceptionLog;
import com.bsj.power.common.def.vo.sys.exceptionlog.ISysExceptionLogSearchVO;
import com.bsj.power.common.def.vo.sys.exceptionlog.OSysExceptionLogPageVO;
import com.bsj.power.common.mapper.SysExceptionLogMapper;
import com.bsj.power.system.service.SysExceptionLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhuojie.c
 * @version V1.0
 * @Description: TODO
 * @date Date : 2024年08月22日
 */
@Service
public class SysExceptionLogServiceImpl implements SysExceptionLogService {

    @Resource
    private SysExceptionLogMapper exceptionLogMapper;

    @Override
    public JsonResult<List<OSysExceptionLogPageVO>> exceptionList(ISysExceptionLogSearchVO exceptionLogPageVO) {
        IPage<SysExceptionLog> page = new Page<>(exceptionLogPageVO.getPageNumber(), exceptionLogPageVO.getPageSize());
        QueryWrapper<SysExceptionLog> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(exceptionLogPageVO.getName())) {
            qw.eq("name", exceptionLogPageVO.getName());
        }
        if (StringUtils.isNotBlank(exceptionLogPageVO.getIp())) {
            qw.eq("ip", exceptionLogPageVO.getIp());
        }
        if (exceptionLogPageVO.getExceptionType() != null && exceptionLogPageVO.getExceptionType() != 0) {
            qw.eq("exception_type", exceptionLogPageVO.getExceptionType());
        }
        qw.between("create_time", exceptionLogPageVO.getStartTime(), exceptionLogPageVO.getEndTime());
        qw.orderByDesc("create_time");
        IPage<SysExceptionLog> iPage = exceptionLogMapper.selectPage(page, qw);
        List<SysExceptionLog> exceptionLogList = iPage.getRecords();
        long total = iPage.getTotal();
        List<OSysExceptionLogPageVO> exceptionLogPageList = new ArrayList<>();
        exceptionLogList.forEach(v -> {
            OSysExceptionLogPageVO oExceptionLogPageVO = new OSysExceptionLogPageVO();
            BeanUtils.copyProperties(v, oExceptionLogPageVO);
            exceptionLogPageList.add(oExceptionLogPageVO);
        });
        return JsonResult.success(exceptionLogPageList, total);
    }
}
